import {createStore} from "redux";


//定义action
function save_collectList_action(item)
{
    return {
        type:"SAVE_COLLECTLIST",
        item
    }
}






const data = {
    collectList:[]
}

function reducer(state=data,action){
    state = localStorage.getItem("state")?JSON.parse(localStorage.getItem("state")):JSON.parse(JSON.stringify(state))
    switch(action.type)
    {
        case"SAVE_COLLECTLIST":
        //将用户收藏的店铺单独存储在store
        let findInd = state.collectList.findIndex(item=>{
            return item.id === action.item.id
        })
        if(findInd === -1){
            state.collectList.push(action.item)
        }else{
            state.collectList.splice(findInd,1)
        }
        localStorage.setItem("state",JSON.stringify(state))
        return state



        default:
            return state
    }
}

const store = createStore(reducer)

export {
    store,
    save_collectList_action
}